import smtplib
from email.mime.text import MIMEText
from email.header import Header

from_addr = '1936260727@qq.com'
password = 'eanbtiaahsfkefda'
to_addr = '1936260727@qq.com'
host = 'smtp.qq.com'

msg = MIMEText("使用python发送纯文本邮件",'plain','utf-8')
msg['From'] = Header(from_addr)
msg['To'] = Header(to_addr)
subject = "Python SMTP 邮件测试"
msg['Subject'] = Header(subject,'utf-8')

# smtplibObj = smtplib.SMTP_SSL(host,465)
# smtplibObj.login(from_addr,password)
# smtplibObj.sendmail(from_addr,to_addr,msg.as_string())
# print("邮件发送成功！")
# smtplibObj.quit()

try:
    with smtplib.SMTP_SSL(host, 465) as smtpobj:
        smtpobj.login(from_addr, password)
        smtpobj.sendmail(from_addr, to_addr, msg.as_string())
        smtpobj.quit()
    print("邮件发送成功！")
except smtplib.SMTPException as  e:
    print("邮件发送失败",e)
